#import "SEGcustomContentView.h"

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif


#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

#import "SEGPlatformManager.h"


/**************************SEGcustomContentView************************/
@implementation SEGcustomContentView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kcustomContentViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.contentTextLabel];

    [self addSubview:self.neighbourView];
    [self addSubview:self.marketView];
    [self addSubview:self.focusView];
    [self addSubview:self.topicTextView];
    [self addSubview:self.topicPicView];
    [self addSubview:self.marketDetailView];
    
    [self addSubview:self.likeLabel];
    [self addSubview:self.commentLabel];
    
    
    self.userInteractionEnabled = YES;
    
//    self.backgroundColor = kColor_T;
//    self.neighbourView.backgroundColor = kColor_S3;
//    self.marketView.backgroundColor = kColor_S3;
//    self.topicTextView.backgroundColor = kColor_S3;
//    self.topicPicView.backgroundColor = kColor_S3;
//    self.marketDetailView.backgroundColor = kColor_S1;
    
    return self;
}

-(void)setWithLayout:(SEGcustomContentViewLayout *)layout{
    self.height = layout.totalHeight;
    self.width = layout.data.widgetWidth;
    
    _layout = layout;
    
    if (self.height <= 0) return;
    
    self.contentTextLabel.textLayout = layout.contentTextLabelTextLayout;
    self.likeLabel.textLayout = layout.likeLabelTextLayout;
    self.commentLabel.textLayout = layout.commentLabelTextLayout;
    
    [self setupUI];
    
    [self.neighbourView setWithLayout:layout.neighbourLayout];
    [self.marketView setWithLayout:layout.marketLayout];
    [self.focusView setWithLayout:layout.focusLayout];
    [self.topicTextView setWithLayout:layout.topicTextLayout];
    [self.topicPicView setWithLayout:layout.topicPicLayout];
    [self.marketDetailView setWithLayout:layout.marketDetailLayout];
}

//界面布局
-(void)setupUI{
    //默认全部隐藏，根据不同类型显示需要控件
    self.contentTextLabel.hidden = YES;
    self.likeLabel.hidden = YES;
    self.commentLabel.hidden = YES;
    
    self.neighbourView.hidden = YES;
    self.marketView.hidden = YES;
    self.focusView.hidden = YES;
    self.topicTextView.hidden = YES;
    self.topicPicView.hidden = YES;
    self.marketDetailView.hidden = YES;
    //邻里圈发布图文
    if (_layout.type == SEGEnumcustomContentViewType_NEIGHBOUR_PHOTOText) {
        
        [self setupUI_NeighbourPhotoText];
    }
    
    //话题参与仅文本
    if (_layout.type == SEGEnumcustomContentViewType_TOPIC_TEXTONLY) {
        [self setupUI_TopicTextOnly];
    }
    
    //话题参与有图文
    if (_layout.type == SEGEnumcustomContentViewType_TOPIC_PHOTOText) {
        [self setupUI_TopicPhotoText];
    }
    
    //集市类列表的集市
    if (_layout.type == SEGEnumcustomContentViewType_MARKET) {
        [self setupUI_Market];
    }
    
    //看点（原PGC）转发
    if (_layout.type == SEGEnumcustomContentViewType_FOCUS) {
        [self setupUI_Focus];
    }
    
    //喜欢（原收藏）
    if (_layout.type == SEGEnumcustomContentViewType_LIKE) {
        [self setupUI_Like];
    }
    
    //集市类列表的集市
    if (_layout.type == SEGEnumcustomContentViewType_MARKETDETAIL) {
        [self setupUI_MarketDetail];
    }
    
    self.contentTextLabel.textAlignment = NSTextAlignmentLeft;
    self.contentTextLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
    self.likeLabel.textAlignment = NSTextAlignmentLeft;
    self.likeLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
    self.commentLabel.textAlignment = NSTextAlignmentLeft;
    self.commentLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
}

-(void)setupUI_NeighbourPhotoText{
    self.contentTextLabel.hidden = NO;
    self.neighbourView.hidden = NO;
    
    //内容文本
    [self.contentTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(_layout.contentTextLabelHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
    
    [self.neighbourView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self.contentTextLabel.mas_bottom);
        make.height.mas_equalTo(_layout.neighbourLayout.totalHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
    
    if (!_layout.data.isDetailView) {//邻里圈详情界面不显示评论点赞按钮
        self.commentLabel.hidden = NO;
        self.likeLabel.hidden = NO;
        //评论
        [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.neighbourView.mas_bottom).offset(_layout.commentTopPadding);
            make.left.equalTo(self).offset(-buttonPadding);
            make.height.mas_equalTo(_layout.commentLabelHeight);
            make.width.mas_equalTo(_layout.commentLabelWidth);
        }];
        
        //点赞
        [self.likeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.commentLabel.mas_bottom);
            make.left.equalTo(self.commentLabel.mas_right).offset(40);
            make.height.mas_equalTo(_layout.likeLabelHeight);
            make.width.mas_equalTo(_layout.likeLabelWidth);
        }];
    }
}

-(void)setupUI_TopicTextOnly{
    self.topicTextView.hidden = NO;
    
    [self.topicTextView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(_layout.topicTextLayout.totalHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
    
    if (!_layout.data.isDetailView) {//邻里圈详情界面不显示评论点赞按钮
        self.commentLabel.hidden = NO;
        self.likeLabel.hidden = NO;
        
        //评论
        [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topicTextView.mas_bottom).offset(_layout.commentTopPadding);
//            make.left.equalTo(self).offset(-buttonPadding);
//            make.height.mas_equalTo(_layout.commentLabelHeight);
//            make.width.mas_equalTo(_layout.commentLabelWidth);
            make.left.equalTo(self).offset(-buttonPadding);
            make.height.mas_equalTo(_layout.commentLabelHeight);
            make.width.mas_equalTo(_layout.commentLabelWidth);
        }];
        
        //点赞
        [self.likeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.commentLabel.mas_bottom);
//            make.left.equalTo(self.commentLabel.mas_right).offset(40);
//            make.height.mas_equalTo(_layout.likeLabelHeight);
//            make.width.mas_equalTo(_layout.likeLabelWidth);
            make.left.equalTo(self.commentLabel.mas_right).offset(40);
            make.height.mas_equalTo(_layout.likeLabelHeight);
            make.width.mas_equalTo(_layout.likeLabelWidth);
        }];
    }
}

-(void)setupUI_TopicPhotoText{
    self.contentTextLabel.hidden = NO;
    self.topicPicView.hidden = NO;
    
    //内容文本（标题）
    [self.contentTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(_layout.contentTextLabelHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
    
    [self.topicPicView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self.contentTextLabel.mas_bottom).mas_offset(viewPadding);
        make.right.equalTo(self.contentTextLabel.mas_right);
        make.height.mas_equalTo(_layout.topicPicLayout.totalHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
    
    if (!_layout.data.isDetailView) {//邻里圈详情界面不显示评论点赞按钮
        self.commentLabel.hidden = NO;
        self.likeLabel.hidden = NO;
        
        //评论
        [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topicPicView.mas_bottom).offset(_layout.commentTopPadding);
            make.left.equalTo(self).offset(-buttonPadding);
            make.height.mas_equalTo(_layout.commentLabelHeight);
            make.width.mas_equalTo(_layout.commentLabelWidth);
        }];
        
        //点赞
        [self.likeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.commentLabel.mas_bottom);
            make.left.equalTo(self.commentLabel.mas_right).offset(40);
            make.height.mas_equalTo(_layout.likeLabelHeight);
            make.width.mas_equalTo(_layout.likeLabelWidth);
        }];
    }

}

-(void)setupUI_Market{
    self.marketView.hidden = NO;
    [self.marketView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.height.mas_equalTo(_layout.marketLayout.totalHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
}

-(void)setupUI_Focus{
    self.contentTextLabel.hidden = NO;
    self.focusView.hidden = NO;
    
    //内容文本（标题）
    [self.contentTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(_layout.contentTextLabelHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
    
    [self.focusView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self.contentTextLabel.mas_bottom).mas_offset(10);
        make.right.equalTo(self.contentTextLabel.mas_right);
        make.height.mas_equalTo(_layout.focusLayout.totalHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
    
    if (!_layout.data.isDetailView) {//邻里圈详情界面不显示评论点赞按钮
        self.commentLabel.hidden = NO;
        self.likeLabel.hidden = NO;
        //评论
        [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.focusView.mas_bottom).mas_offset(_layout.commentTopPadding);
            make.left.equalTo(self).offset(-buttonPadding);
            make.height.mas_equalTo(_layout.commentLabelHeight);
            make.width.mas_equalTo(_layout.commentLabelWidth);
        }];
        
        //点赞
        [self.likeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.commentLabel.mas_bottom);
            make.left.equalTo(self.commentLabel.mas_right).offset(40);
            make.height.mas_equalTo(_layout.likeLabelHeight);
            make.width.mas_equalTo(_layout.likeLabelWidth);
        }];
    }
}

-(void)setupUI_Like{
    self.focusView.hidden = NO;
    
    [self.focusView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self);
        make.height.mas_equalTo(_layout.focusLayout.totalHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
}

- (void)setupUI_MarketDetail
{
    self.marketDetailView.hidden = NO;
    
    [self.marketDetailView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(_layout.marketDetailLayout.totalHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
    
    if (!_layout.data.isDetailView) {//邻里圈详情界面不显示评论点赞按钮
        self.commentLabel.hidden = NO;
        //评论
        [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.marketDetailView.mas_bottom).offset(_layout.commentTopPadding);
            make.left.equalTo(self).offset(-buttonPadding);
            make.height.mas_equalTo(_layout.commentLabelHeight);
            make.width.mas_equalTo(_layout.commentLabelWidth);
        }];
    }
}

#pragma mark - getter
-(SEGLabel *)contentTextLabel{
    if (!_contentTextLabel) {
        _contentTextLabel = [[SEGLabel alloc]init];
        _contentTextLabel.userInteractionEnabled = NO;
        _contentTextLabel.textAlignment = NSTextAlignmentCenter;
        _contentTextLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _contentTextLabel.displaysAsynchronously = YES;
        _contentTextLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _contentTextLabel.fadeOnHighlight = NO;
        _contentTextLabel.numberOfLines = 0;
        _contentTextLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentTextLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentTextLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        //if (kDebug)_contentTextLabel.backgroundColor = kSegRandomColor;
    }
    return _contentTextLabel;
}

-(SEGLabel *)likeLabel{
    if (!_likeLabel) {
        _likeLabel = [[SEGLabel alloc]init];
        _likeLabel.userInteractionEnabled = YES;
        _likeLabel.textAlignment = NSTextAlignmentCenter;
        _likeLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _likeLabel.displaysAsynchronously = YES;
        _likeLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _likeLabel.fadeOnHighlight = NO;
        _likeLabel.numberOfLines = 0;
        _likeLabel.fadeOnAsynchronouslyDisplay = NO;
        _likeLabel.fadeOnAsynchronouslyDisplay = NO;
        _likeLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        //if (kDebug) _likeLabel.backgroundColor = kSegRandomColor;
        __weak typeof(self) _self = self;
        _likeLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(customContentViewLikeLabel:)]) {
                [_self.delegate customContentViewLikeLabel:_self.likeLabel];
            }
        };
    }
    return _likeLabel;
}

-(SEGLabel *)commentLabel{
    if (!_commentLabel) {
        _commentLabel = [[SEGLabel alloc]init];
        _commentLabel.userInteractionEnabled = YES;
        _commentLabel.textAlignment = NSTextAlignmentCenter;
        _commentLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _commentLabel.displaysAsynchronously = YES;
        _commentLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _commentLabel.fadeOnHighlight = NO;
        _commentLabel.numberOfLines = 0;
        _commentLabel.fadeOnAsynchronouslyDisplay = NO;
        _commentLabel.fadeOnAsynchronouslyDisplay = NO;
        _commentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        //if (kDebug)_commentLabel.backgroundColor = kSegRandomColor;
        __weak typeof(self) _self = self;
        _commentLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(customContentViewCommentLabel:)]) {
                [_self.delegate customContentViewCommentLabel:_self.commentLabel];
            }
        };
    }
    return _commentLabel;
}

-(SEGNeighbourView *)neighbourView{
    if (!_neighbourView){
        _neighbourView = [[SEGNeighbourView alloc]init];
        //if (kDebug)_neighbourView.backgroundColor = kSegRandomColor;
    }
    return _neighbourView;
}

-(SEGMarketView *)marketView{
    if (!_marketView){
        _marketView = [[SEGMarketView alloc]init];
        //if (kDebug)_marketView.backgroundColor = kSegRandomColor;
        _marketView.userInteractionEnabled = YES;
        __weak typeof(self) _self = self;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(customContentViewMarketView:)]) {
                [_self.delegate customContentViewMarketView:_marketView];
            }
        }];
        [_marketView addGestureRecognizer:tap];
    }
    return _marketView;
}

-(SEGFocusView *)focusView{
    if (!_focusView){
        _focusView = [[SEGFocusView alloc]init];

        //if (kDebug)_focusView.backgroundColor = kSegRandomColor;
        _focusView.userInteractionEnabled = YES;
        
//         __weak typeof(self) _self = self;
//         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
//             if (_self.delegate && [_self.delegate respondsToSelector:@selector(customContentViewFoucusView:)]) {
//                 [_self.delegate customContentViewFoucusView:_focusView];
//             }
//         }];
//         [_focusView addGestureRecognizer:tap];
    }
    return _focusView;
}

- (SEGTopicTextView *)topicTextView
{
    if (!_topicTextView){
        _topicTextView = [[SEGTopicTextView alloc]init];
        _topicTextView.userInteractionEnabled = YES;
        //if (kDebug)_topicTextView.backgroundColor = kSegRandomColor;
    }
    return _topicTextView;
}

- (SEGTopicPicView *)topicPicView
{
    if (!_topicPicView){
        _topicPicView = [[SEGTopicPicView alloc]init];
        _topicPicView.userInteractionEnabled = YES;
        //if (kDebug)_topicPicView.backgroundColor = kSegRandomColor;
    }
    return _topicPicView;
}


-(SEGMarketDetailView *)marketDetailView{
    if (!_marketDetailView){
        _marketDetailView = [[SEGMarketDetailView alloc]init];
        _marketDetailView.userInteractionEnabled = YES;
        //if (kDebug)_marketDetailView.backgroundColor = kSegRandomColor;
    }
    return _marketDetailView;
}
#pragma mark - hitTest
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    // 01.判断当前控件能否接收事件
    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) {
        return nil;
    }
    // 02.判断点是不是在当前控件上面
    if ([self pointInside:point withEvent:event] == NO) {
        //不再当前视图上 消失
        return nil;
    }
    // 03.从后往前遍历自己的子控件
    for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
        CGPoint convertedPoint = [subview convertPoint:point fromView:self];
        UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event];
        if (hitTestView) {
            //点击点赞和评论按钮 不相应
            if (hitTestView == self.likeLabel||hitTestView == self.commentLabel) {
                return nil;
            }
            return hitTestView;
        }
    }
    return nil;
}
@end
